/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.jinyao.exploit.modules.gen.web;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jinyao.exploit.common.base.web.BaseController;
import com.jinyao.exploit.common.base.User;
import com.jinyao.exploit.common.utils.StringUtils;
import com.jinyao.exploit.common.utils.UserUtils;
import com.jinyao.exploit.modules.gen.entity.Scheme;
import com.jinyao.exploit.modules.gen.service.SchemeService;
import com.jinyao.exploit.modules.gen.service.TableService;
import com.jinyao.exploit.modules.gen.util.GenUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 生成方案Controller
 * @author ThinkGem
 * @version 2013-10-15
 */
@Controller
@RequestMapping(value = "${baseAdminPath}/gen/scheme")
public class SchemeController extends BaseController {

	@Resource(name = "schemeServiceImpl")
	private SchemeService schemeService;
	@Resource(name = "tableServiceImpl")
	private TableService tableService;
	
	@ModelAttribute
	public Scheme get(@RequestParam(required=false) long id) {
		if (id != 0){
			return schemeService.getById(id);
		}else{
			return new Scheme();
		}
	}
	
	@RequestMapping(value = {"list", ""})
	public String list(Scheme scheme, HttpServletRequest request, HttpServletResponse response, Model model) {
        IPage<Scheme> page = schemeService.page(new Page<>());
        model.addAttribute("page", page);
		
		return "modules/gen/schemeList";
	}

	@RequestMapping(value = "form")
	public String form(Scheme scheme, Model model) {
		if (StringUtils.isBlank(scheme.getPackageName())){
			scheme.setPackageName("com.jinyao.exploit.modules");
		}
//		if (StringUtils.isBlank(scheme.getFunctionAuthor())){
//			scheme.setFunctionAuthor(UserUtils.getUser().getName());
//		}
		model.addAttribute("scheme", scheme);
		model.addAttribute("config", GenUtils.getConfig());
		model.addAttribute("tableList", tableService.list());
		return "modules/gen/schemeForm";
	}

	@RequestMapping(value = "save")
	public String save(Scheme scheme, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, scheme)){
			return form(scheme, model);
		}
		
		schemeService.save(scheme);
		addMessage(redirectAttributes, "操作生成方案'" + scheme.getName() + "'成功<br/>" + scheme.getGenerateCode());
		return "redirect:" + adminPath + "/gen/scheme/?repage";
	}
	
	@RequestMapping(value = "delete")
	public String delete(Scheme scheme, RedirectAttributes redirectAttributes) {
		schemeService.removeById(scheme);
		addMessage(redirectAttributes, "删除生成方案成功");
		return "redirect:" + adminPath + "/gen/scheme/?repage";
	}

}
